home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / wb / flynn / source / tools.c < prev    next >
C/C++ Source or Header  |  1999-06-15  |  7KB  |  447 lines

  1.  
  2. /*********************************************************************
  3. ----------------------------------------------------------------------
  4.  
  5.     tools
  6.  
  7. ----------------------------------------------------------------------
  8. *********************************************************************/
  9.  
  10. #include <string.h>
  11. #include <stdio.h>
  12.  
  13. #include <exec/lists.h>
  14. #include <dos/dos.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <proto/utility.h>
  18.  
  19. #include "global.h"
  20.  
  21.  
  22. /*********************************************************************
  23.  
  24.     s' = StrDup(s)
  25.  
  26. *********************************************************************/
  27.  
  28. char *StrDup(char *s)
  29. {
  30.     char *s2 = NULL;
  31.  
  32.     if (s)
  33.     {
  34.         int l = strlen(s);
  35.         if (s2 = Malloc(l+1))
  36.         {
  37.             strcpy(s2, s);
  38.         }
  39.     }
  40.     
  41.     return s2;
  42. }
  43.  
  44.  
  45.  
  46. /*********************************************************************
  47.  
  48.     node = DeleteNode(node)
  49.  
  50.     delete a named node
  51.  
  52. *********************************************************************/
  53.  
  54. void DeleteNode(struct Node *node)
  55. {
  56.     if (node)
  57.     {
  58.         Free(node->ln_Name);
  59.         Free(node);
  60.     }
  61. }
  62.  
  63.  
  64. /*********************************************************************
  65.  
  66.     node = CreateNode(char *name)
  67.  
  68.     create a named node
  69.  
  70. *********************************************************************/
  71.  
  72. struct Node *CreateNode(char *name)
  73. {
  74.     struct Node *node;
  75.     BOOL success = FALSE;
  76.  
  77.     if (node = Malloclear(sizeof(struct Node)))
  78.     {
  79.         node->ln_Type = NT_USER;
  80.         node->ln_Pri = 0;
  81.         if (node->ln_Name = StrDup(name))
  82.         {
  83.             success = TRUE;
  84.         }
  85.     }
  86.     
  87.     if (!success)
  88.     {
  89.         DeleteNode(node);
  90.         node = NULL;
  91.     }
  92.     
  93.     return node;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. /*********************************************************************
  100.  
  101.     list = CreateList(void)
  102.  
  103.     create a double linked list
  104.  
  105. *********************************************************************/
  106.  
  107. struct List *CreateList(void)
  108. {
  109.     struct List *list;
  110.  
  111.     if (list = Malloc(sizeof(struct List)))
  112.     {
  113.         NewList(list);
  114.     }
  115.     
  116.     return list;
  117. }
  118.  
  119.  
  120.  
  121. /*********************************************************************
  122.  
  123.     DeleteList(void)
  124.  
  125.     free a list
  126.  
  127. *********************************************************************/
  128.  
  129. void DeleteList(struct List *list)
  130. {
  131.     if (list)
  132.     {
  133.         struct Node *node;
  134.         while (!IsListEmpty(list))
  135.         {
  136.             if (node = list->lh_Head)
  137.             {
  138.                 Remove(node);
  139.                 DeleteNode(node);
  140.             }
  141.         }
  142.         Free(list);
  143.     }
  144. }
  145.  
  146.  
  147.  
  148. /*********************************************************************
  149.  
  150.     list = AllocStringArray(entries)
  151.     create a stringarray with a given number of entries
  152.  
  153. *********************************************************************/
  154.  
  155. char **AllocStringArray(int num)
  156. {
  157.     return Malloclear((num + 1) * sizeof(char *));
  158. }
  159.  
  160.  
  161.  
  162. /*********************************************************************
  163.  
  164.     FreeStringArray(list)
  165.     delete a list of strings
  166.  
  167. *********************************************************************/
  168.  
  169. void FreeStringArray(char **list)
  170. {
  171.     if (list)
  172.     {
  173.         char **p = list;
  174.         while(*p)
  175.         {
  176.             Free(*p++);
  177.         }
  178.         Free(list);
  179.     }
  180. }
  181.  
  182.  
  183. /*********************************************************************
  184.  
  185.     list = DupStringArray(list)
  186.  
  187.     create a duplicate of a stringarray
  188.  
  189. *********************************************************************/
  190.  
  191. char **DupStringArray(char **list)
  192. {
  193.     char **newlist = NULL;
  194.     if (list)
  195.     {
  196.         int i, num = 0;
  197.         char **p = list;
  198.  
  199.         while (*p++) num++;
  200.  
  201.         if (newlist = AllocStringArray(num))
  202.         {    
  203.             for (i = 0; i < num; ++i)
  204.             {
  205.                 if (!(newlist[i] = StrDup(list[i])))
  206.                 {
  207.                     FreeStringArray(newlist);
  208.                     newlist = NULL;
  209.                     break;
  210.                 }
  211.             }
  212.         }
  213.     }
  214.     
  215.     return newlist;
  216. }
  217.  
  218.  
  219. /*********************************************************************
  220.  
  221.     sl = CreateStringArray(...)
  222.  
  223.     create a stringarray with given initializers
  224.  
  225. *********************************************************************/
  226.  
  227. char **CreateStringArray(char *s, ...)
  228. {
  229.     char **sl;
  230.     char **t;
  231.     int c = 1;
  232.     
  233.     t = &s;
  234.     while (*t++)
  235.     {
  236.         c++;
  237.     }
  238.  
  239.     if (sl = Malloclear(c * sizeof(char *)))
  240.     {
  241.         int i = 0;
  242.         BOOL success = TRUE;
  243.         t = &s;
  244.         while (*t && success)
  245.         {
  246.             success = !!(sl[i++] = StrDup(*t));
  247.             t++;
  248.         }
  249.         
  250.         if (!success)
  251.         {
  252.             FreeStringArray(sl);
  253.             sl = NULL;
  254.         }
  255.     }
  256.  
  257.     return sl;
  258. }
  259.  
  260. /*********************************************************************
  261.  
  262.     comp = StriCmp(s1, s2)
  263.     
  264.     localized, case-insensitive string comparison,
  265.     handles NULL strings
  266.  
  267. *********************************************************************/
  268.  
  269. LONG StriCmp(char *s1, char *s2)
  270. {
  271.     if (s1 && s2)
  272.     {
  273.         return Stricmp(s1, s2);
  274.     }
  275.  
  276.     if (s1 == NULL && s2 == NULL)
  277.     {
  278.         return 0;
  279.     }
  280.     
  281.     return -1;
  282. }
  283.  
  284.  
  285.  
  286. /*********************************************************************
  287.  
  288.     count = CountListEntries(list)
  289.  
  290.     count number of entries in a list
  291.  
  292. *********************************************************************/
  293.  
  294. int CountListEntries(struct List *list)
  295. {
  296.     int count = 0;
  297.  
  298.     if (list)
  299.     {
  300.         if (!IsListEmpty(list))
  301.         {
  302.             struct Node *node;
  303.             struct Node *nextnode;
  304.  
  305.             node = list->lh_Head;
  306.             while (nextnode = node->ln_Succ)
  307.             {
  308.                 count++;
  309.                 node = nextnode;
  310.             }
  311.         }
  312.     }
  313.  
  314.     return count;
  315. }
  316.  
  317.  
  318.  
  319. /*********************************************************************
  320.  
  321.     stringlist = CreateStringArrayFromList(list)
  322.  
  323.     create a stringarray from a list
  324.  
  325. *********************************************************************/
  326.  
  327. char **CreateStringArrayFromList(struct List *list)
  328. {
  329.     char **array = NULL;
  330.  
  331.     int count;
  332.  
  333.     if (count = CountListEntries(list))
  334.     {
  335.         if (array = AllocStringArray(count))
  336.         {
  337.             struct Node *node;
  338.             struct Node *nextnode;
  339.             int i = 0;
  340.             BOOL success = TRUE;
  341.  
  342.             node = list->lh_Head;
  343.             while ((nextnode = node->ln_Succ) && success)
  344.             {
  345.                 success = !!(array[i++] = StrDup(node->ln_Name));
  346.                 node = nextnode;
  347.             }
  348.             
  349.             if (!success)
  350.             {
  351.                 FreeStringArray(array);
  352.                 array = NULL;
  353.             }
  354.         }
  355.     }
  356.  
  357.     return array;
  358. }
  359.  
  360. /*********************************************************************
  361.  
  362.     size = FileSize(filename);
  363.  
  364. *********************************************************************/
  365.  
  366. long FileSize(char *filename)
  367. {
  368.     long filesize = -1;
  369.  
  370.     if (filename)
  371.     {
  372.         BPTR lock;
  373.         if (lock = Lock(filename, ACCESS_READ))
  374.         {
  375.             struct FileInfoBlock *fib;
  376.  
  377.             if (fib = AllocDosObject(DOS_FIB, NULL))
  378.             {
  379.                 if (Examine(lock, fib))
  380.                 {
  381.                     if (fib->fib_DirEntryType < 0)
  382.                     {
  383.                         filesize = fib->fib_Size;
  384.                     }
  385.                 }
  386.  
  387.                 FreeDosObject(DOS_FIB, fib);
  388.             }
  389.  
  390.             UnLock(lock);    
  391.         }
  392.     }
  393.  
  394.     return filesize;
  395. }
  396.  
  397.  
  398. /*********************************************************************
  399.  
  400.     list = CreateListFromArray(array)
  401.  
  402.     create a double linked list from an array
  403.  
  404. *********************************************************************/
  405.  
  406. struct List *CreateListFromArray(char **array)
  407. {
  408.     struct List *list;
  409.  
  410.     if (list = CreateList())
  411.     {
  412.         char **t;
  413.  
  414.         if (t = array)
  415.         {
  416.             BOOL error = FALSE;
  417.             struct Node *node;
  418.  
  419.             while (*t && !error)
  420.             {
  421.                 error = TRUE;
  422.                 if (node = Malloclear(sizeof(struct Node)))
  423.                 {
  424.                     if (node->ln_Name = StrDup(*t))
  425.                     {
  426.                         AddTail(list, node);
  427.                         error = FALSE;                    
  428.                     }
  429.                     else
  430.                     {
  431.                         Free(node);
  432.                     }
  433.                 }
  434.                 t++;
  435.             }
  436.             
  437.             if (error)
  438.             {
  439.                 DeleteList(list);
  440.                 list = NULL;
  441.             }
  442.         }
  443.     }
  444.  
  445.     return list;
  446. }
  447.